package com.xcjw.com.Utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.xcjw.com.MainActivity;

/**
 * @packname com.homeclient.com.Utils
 * @filename KeybordUtils
 * @date on 2018/10/24 12:54
 *****/

public class KeyboardUtils {

      public static void showKeyboard(View view) {
            InputMethodManager imm = (InputMethodManager) view.getContext()
                      .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                  view.requestFocus();
                  imm.showSoftInput(view, 0);
            }
      }

      public static void hideKeyboard(View view) {
            InputMethodManager imm = (InputMethodManager) view.getContext()
                      .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                  imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
      }

      public static void toggleSoftInput(View view) {
            InputMethodManager imm = (InputMethodManager) view.getContext()
                      .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                  imm.toggleSoftInput(0, 0);
            }
      }

      public static boolean isSoftShowing(Activity context) {
            //获取当屏幕内容的高度
            int screenHeight = context.getWindow().getDecorView().getHeight();
            //获取View可见区域的bottom
            Rect rect = new Rect();
            //DecorView即为activity的顶级view
            context.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
            //考虑到虚拟导航栏的情况（虚拟导航栏情况下：screenHeight = rect.bottom + 虚拟导航栏高度）
            //选取screenHeight*2/3进行判断
            return screenHeight*2/3 > rect.bottom;
      }
      public  static void close(Activity activity){
            if (isSoftShowing(activity)) {
                  final View v = (activity).getWindow().peekDecorView();
                  if (v != null && v.getWindowToken() != null) {
                        InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                  }
            }

      }

}
